package com.test.listview;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class GridViewActivity extends Activity implements OnItemClickListener{
	/** Called when the activity is first created. */

	private GridView mGridView;
	private TempBaseAdapter mAdapter;
	private PackageManager pm;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gridview);
		pm = getPackageManager();
		findAndSettings();
	}

	/**
	 * 查找和设置
	 */
	private void findAndSettings() {
		mGridView = (GridView) findViewById(R.id.gridview);
		mAdapter = new TempBaseAdapter(this, queryMainIntentActivity(pm));
		mGridView.setAdapter(mAdapter);
		mGridView.setOnItemClickListener(this);
	}

	private class TempBaseAdapter extends BaseAdapter {

		private Context mContext;
		private LayoutInflater mInflater;
		private List<ResolveInfo> mData;

		public TempBaseAdapter(Context context, List<ResolveInfo> list) {
			mContext = context;
			mInflater = LayoutInflater.from(context);
			mData = list;
		}

		@Override
		public int getCount() {
			return mData == null ? 0 : mData.size();
		}

		@Override
		public Object getItem(int position) {
			if (mData == null || mData.size() == 0) {
				return null;
			} else {
				return mData.get(position);
			}
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			ViewHolder holder;
			if (convertView == null){
				convertView = mInflater.inflate(R.layout.gridview_item, null);
				holder = new ViewHolder();
				holder.imgView = (ImageView)convertView.findViewById(R.id.img);
				holder.titleTextView = (TextView)convertView.findViewById(R.id.title);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}

			System.out.println("getView() position==" + position + ",convertView==" + convertView);

			ResolveInfo info = mData.get(position);
			holder.imgView.setBackgroundDrawable(info.loadIcon(pm));
			holder.titleTextView.setText(info.loadLabel(pm));
			return convertView;
		}

		class ViewHolder {
			public ImageView imgView;
			public TextView titleTextView;
		}
	}

	/**
	 * 查询非系统的应用
	 * @param pm
	 * @return
	 */
	public List<ResolveInfo> queryMainIntentActivity(PackageManager pm) {
		final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
		mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);

		List<ResolveInfo> result = null;
		List<ResolveInfo> list = pm.queryIntentActivities(mainIntent, 0);
		if (list != null && list.size() > 0){
			result = new ArrayList<ResolveInfo>();
			for (ResolveInfo info : list){
//				if (!isSystemApp(info.activityInfo.applicationInfo)){
//					result.add(info);
//				}
				result.add(info);
			}
		}

		return result;
	}

	/**
	 * 是否是系统软件或者是系统软件的更新软件
	 * @return
	 */
	public boolean isSystemApp(ApplicationInfo appInfo) {
		return ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) != 0);
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
							long id) {
		ResolveInfo info = (ResolveInfo)mAdapter.getItem(position);
		Toast toast = Toast.makeText(this, "点击了：" + info.loadLabel(pm), Toast.LENGTH_SHORT);
		toast.show();
	}
}